

pub trait IBytesMessage {
    // fn from_slice(slice: &[u8]) -> Self;
    fn from_bytes(blob: Vec<u8>) -> Self;
    fn to_bytes(self) -> Vec<u8>;
}


impl IBytesMessage for String {
    // fn from_slice(slice: &[u8]) -> Self {
    //     match Self::from_utf8_lossy(slice) {
    //         Ok(v) => v,
    //         Err(e) => format!("error utf8: {:?}", e),
    //     }
    // }
    fn from_bytes(blob: Vec<u8>) -> Self {
        match Self::from_utf8(blob){
            Ok(v) => v,
            Err(e) => format!("error utf8: {:?}", e),
        }
    }

    fn to_bytes(self) -> Vec<u8> {
        self.into_bytes()
    }
}



const LEN_2: usize = 2;
const LEN_4: usize = 4;
const LEN_8: usize = 8;
const LEN_16: usize = 16;

impl IBytesMessage for i8 {
    // fn from_slice(slice: &[u8]) -> Self {
    //     slice[0] as i8
    // }
    fn from_bytes(blob: Vec<u8>) -> Self {
        blob[0] as i8
    }

    fn to_bytes(self) -> Vec<u8> {
        vec![self as u8]
    }
}
impl IBytesMessage for u8 {
    // fn from_slice(slice: &[u8]) -> Self {
    //     slice[0]
    // }

    fn from_bytes(blob: Vec<u8>) -> Self {
        blob[0]
    }

    fn to_bytes(self) -> Vec<u8> {
        vec![self]
    }
}
impl IBytesMessage for i16 {
    fn from_bytes(blob: Vec<u8>) -> Self {
        let mut arr = [0_u8; LEN_2];
        for i in 0..LEN_2 {
            arr[i] = blob[i];
        }
        i16::from_le_bytes(arr)
    }

    fn to_bytes(self) -> Vec<u8> {
        self.to_le_bytes().to_vec()
    }
}
impl IBytesMessage for u16 {
    fn from_bytes(blob: Vec<u8>) -> Self {
        let mut arr = [0_u8; LEN_2];
        for i in 0..LEN_2 {
            arr[i] = blob[i];
        }
        u16::from_le_bytes(arr)
    }

    fn to_bytes(self) -> Vec<u8> {
        self.to_le_bytes().to_vec()
    }
}
impl IBytesMessage for i32 {
    fn from_bytes(blob: Vec<u8>) -> Self {
        let mut arr = [0_u8; LEN_4];
        for i in 0..LEN_4 {
            arr[i] = blob[i];
        }
        i32::from_le_bytes(arr)
    }

    fn to_bytes(self) -> Vec<u8> {
        self.to_le_bytes().to_vec()
    }
}
impl IBytesMessage for u32 {
    fn from_bytes(blob: Vec<u8>) -> Self {
        let mut arr = [0_u8; LEN_4];
        for i in 0..LEN_4 {
            arr[i] = blob[i];
        }
        u32::from_le_bytes(arr)
    }

    fn to_bytes(self) -> Vec<u8> {
        self.to_le_bytes().to_vec()
    }
}

impl IBytesMessage for f32 {
    fn from_bytes(blob: Vec<u8>) -> Self {
        let mut arr = [0_u8; LEN_4];
        for i in 0..LEN_4 {
            arr[i] = blob[i];
        }
        f32::from_le_bytes(arr)
    }

    fn to_bytes(self) -> Vec<u8> {
        self.to_le_bytes().to_vec()
    }
}



impl IBytesMessage for i64 {
    fn from_bytes(blob: Vec<u8>) -> Self {
        let mut arr = [0_u8; LEN_8];
        for i in 0..LEN_8 {
            arr[i] = blob[i];
        }
        i64::from_le_bytes(arr)
    }

    fn to_bytes(self) -> Vec<u8> {
        self.to_le_bytes().to_vec()
    }
}

impl IBytesMessage for u64 {
    fn from_bytes(blob: Vec<u8>) -> Self {
        let mut arr = [0_u8; LEN_8];
        for i in 0..LEN_8 {
            arr[i] = blob[i];
        }
        u64::from_le_bytes(arr)
    }

    fn to_bytes(self) -> Vec<u8> {
        self.to_le_bytes().to_vec()
    }
}
impl IBytesMessage for f64 {
    fn from_bytes(blob: Vec<u8>) -> Self {
        let mut arr = [0_u8; LEN_8];
        for i in 0..LEN_8 {
            arr[i] = blob[i];
        }
        f64::from_le_bytes(arr)
    }

    fn to_bytes(self) -> Vec<u8> {
        self.to_le_bytes().to_vec()
    }
}

impl IBytesMessage for i128 {
    fn from_bytes(blob: Vec<u8>) -> Self {
        let mut arr = [0_u8; LEN_16];
        for i in 0..LEN_16 {
            arr[i] = blob[i];
        }
        i128::from_le_bytes(arr)
    }

    fn to_bytes(self) -> Vec<u8> {
        self.to_le_bytes().to_vec()
    }
}

impl IBytesMessage for u128 {
    fn from_bytes(blob: Vec<u8>) -> Self {
        let mut arr = [0_u8; LEN_16];
        for i in 0..LEN_16 {
            arr[i] = blob[i];
        }
        u128::from_le_bytes(arr)
    }

    fn to_bytes(self) -> Vec<u8> {
        self.to_le_bytes().to_vec()
    }
}